package com.baomidou.springwind.controller.supply;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.kisso.annotation.Permission;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.springwind.controller.BaseController;
import com.baomidou.springwind.entity.NetworkSupply;
import com.baomidou.springwind.service.NetworkSupplyService;
import com.baomidou.springwind.mapper.NetworkSupplyDao;
import com.baomidou.springwind.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 物流供应链
 * </p>
 *
 * @Author huangtao
 * @Date 2017-8-21
 */
@Controller
@RequestMapping("/network/supply")
public class ProductController extends BaseController {

    @Autowired
    private NetworkSupplyService networkSupplyService;
    @Autowired
    private NetworkSupplyDao networkSupplyDao;
    @Autowired
    private ProjectService projectService;
    /**
     * 客户分析主界面
     */
    @RequestMapping("/product")
    @Permission("1001")
    public String customGroup(Model model) {
        EntityWrapper<NetworkSupply> wrapper = new EntityWrapper<NetworkSupply>();
        wrapper.eq("batch_id",getProjectId());
        wrapper.groupBy("branch_center");
        model.addAttribute("prjList", networkSupplyService.selectList(wrapper));
        EntityWrapper<NetworkSupply> wrapper1 = new EntityWrapper<NetworkSupply>();
        wrapper1.eq("batch_id",getProjectId());
        wrapper1.groupBy("delivery_center");
        model.addAttribute("deliveryList", networkSupplyService.selectList(wrapper1));

        EntityWrapper<NetworkSupply> wrapper2 = new EntityWrapper<NetworkSupply>();
        wrapper2.eq("operator_id",getCurrentUserId());
        wrapper2.eq("batch_id",getProjectId());
        wrapper2.groupBy("product_group");
        model.addAttribute("salesList", networkSupplyService.selectList(wrapper2));

        return pjaxPath("/lpp/network/supply/product/main", model);
    }



    @ResponseBody
    @Permission("1001")
    @RequestMapping("/product/bar1")
    public String getBar1(Long batchId, Model model) {
        String center=request.getParameter("center");
        if (center.length()<0||center==""){
            center="海口工贸";
        }
        EntityWrapper<NetworkSupply> wrapper = new EntityWrapper<NetworkSupply>();
        wrapper.eq("branch_center",center);
        wrapper.groupBy("product_group");
        wrapper.setSqlSelect("sum(order_quantity) quantity,product_group product");
        wrapper.eq("batch_id", getProjectId());
        List<Map<String, Object>> mapList = networkSupplyService.selectMaps(wrapper);
        if(mapList.size()==0){
            String message="查询数据异常";
            return callbackFail(message);
        }
        JSONArray dataAxis=new JSONArray();
        JSONArray data=new JSONArray();
        JSONObject a1 = null;
        JSONObject ret = new JSONObject();
        for (int i=0;i<mapList.size();i++){
            a1 = new JSONObject();
            a1.put("name",mapList.get(i).get("product"));
            a1.put("value",mapList.get(i).get("quantity"));
            data.add(a1);
            dataAxis.add(mapList.get(i).get("product"));
        }
        ret.put("x",data);
        ret.put("x2",dataAxis);
        return callbackSuccess(ret);
    }

    @ResponseBody
    @Permission("1001")
    @RequestMapping("/product/bar2")
    public String getBar2(Long batchId, Model model) {
        String center=request.getParameter("center");
        if (center.length()<0||center==""){
            center="厦门";
        }
        EntityWrapper<NetworkSupply> wrapper = new EntityWrapper<NetworkSupply>();
        wrapper.eq("delivery_center",center);
        wrapper.groupBy("product_group");
        wrapper.setSqlSelect("sum(order_quantity) quantity,product_group product");
        wrapper.eq("batch_id", getProjectId());
        List<Map<String, Object>> mapList = networkSupplyService.selectMaps(wrapper);
        if(mapList.size()==0){
            String message="查询数据异常";
            return callbackFail(message);
        }
        JSONArray dataAxis=new JSONArray();
        JSONArray data=new JSONArray();
        JSONObject a1 = null;
        JSONObject ret = new JSONObject();
        for (int i=0;i<mapList.size();i++){
            a1 = new JSONObject();
            dataAxis.add(mapList.get(i).get("product"));
            a1.put("name",mapList.get(i).get("product"));
            a1.put("value",mapList.get(i).get("quantity"));
            data.add(a1);
        }
        ret.put("x",data);
        ret.put("data2",dataAxis);
        return callbackSuccess(ret);
    }

    @ResponseBody
    @Permission("1001")
    @RequestMapping("/product/bar3")
    public String getBar3(Long batchId, Model model) {
        EntityWrapper<NetworkSupply> wrapper = new EntityWrapper<NetworkSupply>();
        wrapper.groupBy("product_group,primary_sales");
        wrapper.setSqlSelect("product_group product,primary_sales sales,sum(order_quantity) quantity");
        wrapper.eq("batch_id", getProjectId());
        List<Map<String, Object>> mapList = networkSupplyService.selectMaps(wrapper);
        if(mapList.size()==0){
            String message="查询数据异常";
            return callbackFail(message);
        }
        JSONArray dataAxis=new JSONArray();
        JSONArray data=new JSONArray();
        JSONArray a1 = new JSONArray();
        JSONObject ret = new JSONObject();
        for (int i=0;i<mapList.size();i++){
            dataAxis.add(mapList.get(i).get("product")+"-"+mapList.get(i).get("sales"));
            a1.add(mapList.get(i).get("quantity"));
        }
        ret.put("x",dataAxis);
        ret.put("a1",a1);
        return callbackSuccess(ret);
    }

    @ResponseBody
    @Permission("1001")
    @RequestMapping("/product/bar4")
    public String getBar4(Long batchId, Model model){
        Map<String, Object> pm = getParameterMap();
        pm.put("batchId", getProjectId());
        pm.put("operatorId",getCurrentUserId());
        List<Map<String, Object>> mapList = networkSupplyDao.selectMonthProduct(pm);
        if(mapList.size()==0){
            String message="查询数据异常";
            return callbackFail(message);
        }
        JSONArray dataAxis=new JSONArray();
        JSONArray a2=new JSONArray();
        JSONArray a1 = new JSONArray();
        JSONArray a3 = new JSONArray();
        JSONObject ret = new JSONObject();
        for (int i=0;i<6;i++){
            a1.add(mapList.get(i).get("quantity"));
            a2.add(mapList.get(i+6).get("quantity"));
            a3.add(mapList.get(i).get("months"));
        }
        ret.put("x",a3);
        ret.put("a1",a1);
        ret.put("a2",a2);
        return callbackSuccess(ret);
    }


}
